app.controller("z-raApproval-c", [ '$scope','$rootScope','$z-http','$sce','z-raApproval-s','z-attendance-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,AttendanceService,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";		  
		$scope.viewModel.approvalState="" ;        //审批状态
		$scope.viewModel.checkAll=false;          //全选状态
		//保存复选框状态
		$scope.viewModel.checkboxDataList=[];
		$scope.viewModel.approvalState="2";
		$scope.viewModel.employeeNo=null;
		$scope.viewModel.employeeList=[];
		
		$scope.editModel= {};
		$scope.editModel.title="";
		$scope.editModel.approvalStateList=[];
		$scope.viewModel.approvalState = "1";
		
		var now = new Date();                     //当前日期     
		var nowMonth = now.getMonth();            //当前月     
		var nowYear=$filter('date')(now,'yyyy');  //当前年
		//获取当月开始日期
		var monthStartDate = $filter("date")(new Date(nowYear, nowMonth, 1), "yyyy/MM/dd");
		//获取当前日期
        var dayEndDate= $filter("date")(now, "yyyy/MM/dd");
		
        $scope.viewModel.retroactiveStratDate=new Date(monthStartDate);   //起止日期
        $scope.viewModel.retroactiveEndDate=new Date(dayEndDate);            //终止日期
        
        
		//日期控件
        $scope.viewModel.dateOptions = {
                formatYear: 'yy',
                startingDay: 1,
                showWeeks:false
        };
        //日期控件
        $scope.viewModel.format = "yyyy/MM/dd";
        //日期启动开关
        $scope.viewModel.startDateOpened = false;
        $scope.viewModel.endDateOpened = false;
		
        //起止日期选择
        $scope.viewModel.startDateOpen = function($event) {
            $event.preventDefault();
            $event.stopPropagation();
             $scope.viewModel.startDateOpened = !$scope.viewModel.startDateOpened;
        };
        //起止日期选择
        $scope.viewModel.endDateOpen = function($event) {
            $event.preventDefault();
            $event.stopPropagation();
             $scope.viewModel.endDateOpened = !$scope.viewModel.endDateOpened;
        };
        
		//查询页面名称
		MenuService.getMenuByUrl({url:'raApproval'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		

		
		/*****************选择框*****************/
		
		//设置默认复选框值
		$scope.viewModel.addCheckbox=function(){
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 $scope.viewModel.dataList[i].checkbox=false;
			 }
		};
		
		//处理加载复选框选择状态
		$scope.viewModel.loadDefaultcheckbox=function(){
			 for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
				 for(var j=0;j<$scope.viewModel.dataList.length;j++){
					 if($scope.viewModel.checkboxDataList[i].retroactiveId==$scope.viewModel.dataList[j].retroactiveId){
						 if($scope.viewModel.checkboxDataList[i].checkbox){
							 $scope.viewModel.dataList[j].checkbox=true;
						 }else{
							 $scope.viewModel.dataList[j].checkbox=false;
						 }
					 }
				 }
			 }
		};
		
		//判断是否为全选
		$scope.viewModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 if($scope.viewModel.dataList[i].approvalState!=2){
					 if($scope.viewModel.dataList[i].checkbox==false){
						 type=false;
						 break;
					 }else{
						 type=true;
					 }
				 }
			 }
			 if(type){
				 $scope.viewModel.checkAll=true;
			 }else{
				 $scope.viewModel.checkAll=false;
			 }
		};
		
		//全选事件
		$scope.viewModel.checkboxAll=function(){
			//判断是否为全选
			 if($scope.viewModel.checkAll==false){
				 angular.forEach($scope.viewModel.dataList, function(value, key) {
					 if(value.approvalState==1){
						 value.checkbox=true;
						 var type=true;
						 for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
							 if($scope.viewModel.checkboxDataList[i].retroactiveId==value.retroactiveId){
								 type=false;
								 break;
							 }else{
								 type=true;
							 }
						 }
						 if(type){
							 $scope.viewModel.checkboxDataList.push(value);
						 }
					 }
				 });
			 }else{
				 angular.forEach($scope.viewModel.dataList, function(value, key) {
					 value.checkbox=false;
					 for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
						 if($scope.viewModel.checkboxDataList[i].retroactiveId==value.retroactiveId){
							 $scope.viewModel.checkboxDataList.splice(i,1);
						 }
					 }
				 });
			 }
		};
		//单选事件
		$scope.viewModel.checkbox=function(raApproval){
			 if(raApproval.checkbox){
				 raApproval.checkbox=false;
				 for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
					 if($scope.viewModel.checkboxDataList[i].retroactiveId==raApproval.retroactiveId){
						 $scope.viewModel.checkboxDataList.splice(i,1);
					 }
				 }
			 }else{
				 raApproval.checkbox=true;
				 $scope.viewModel.checkboxDataList.push(raApproval);
			 }
			 $scope.viewModel.JudgeCheckboxAll();
		};
	
		//加载考勤状态数据
		$scope.viewModel.bindSelectData = function(){
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"10"
			},
			function(data){
				$scope.viewModel.approvalStateList= data;
				 //遍历并判断是否为通过或拒绝
                for(var i=0;i<$scope.viewModel.approvalStateList.length;i++){
                    if($scope.viewModel.approvalStateList[i].dicCd=='2' || $scope.viewModel.approvalStateList[i].dicCd=='3'){
                        $scope.editModel.approvalStateList.push($scope.viewModel.approvalStateList[i]);
                    }
                }
			},function(error) {
						
			});
		};
		$scope.viewModel.bindSelectData();
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.employeeList=[];
			if($scope.viewModel.employeeNo!=null){
				var employeeNoData = $scope.viewModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getRetroactiveApprovalList({
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage,
				employeeList:$scope.viewModel.employeeList,
				approvalState:$scope.viewModel.approvalState,
				retroactiveStratDate:$scope.viewModel.retroactiveStratDate,
				retroactiveEndDate:$scope.viewModel.retroactiveEndDate
			},
			function(data){
				$scope.viewModel.dataList = data.retroactiveApprovalList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.addCheckbox();
				$scope.viewModel.loadDefaultcheckbox();
			    $scope.viewModel.JudgeCheckboxAll();
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};

		//新增
		$scope.viewModel.add = function(retroctive){
			if($scope.viewModel.checkboxDataList.length<=0){
				$rootScope.alertWarning("未选择补签审批信息！");
				return;
			}
			for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
				if($scope.viewModel.checkboxDataList[i].approvalState==2){
					$rootScope.alertWarning("已存在补签审批通过信息！");
					return;
				}
			}
			
			$scope.viewModel.load();
			$scope.editModel.title="补签审批";
			$scope.editModel.raApproval={};
			$scope.editModel.raApproval.approvalState=2;

			//验证不能为空
			$scope.editForm.approvalState.$dirty=false;
		};

		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.approvalState.$dirty){
            		$scope.editForm.approvalState.$dirty=true;
            	}
                return true;
            }else{
            	return false;
            }
        };
        
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.editModel.raApproval.retroactiveIdList=[];
			for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
				var retroactiveId=$scope.viewModel.checkboxDataList[i].retroactiveId;
				$scope.editModel.raApproval.retroactiveIdList[i]=retroactiveId;
			}
			$scope.viewModel.isLoading = true;
			Service.saveRetroactiveApproval($scope.editModel.raApproval,
				function(data){
					$scope.viewModel.unLoad();
					$scope.viewModel.bindData();
					$rootScope.alertInfo("补签审批完成。");
					//刷新铃铛
					$rootScope.rootTotal();
					$scope.viewModel.checkboxDataList=[];
					$scope.viewModel.isLoading = false;
				},function(error) {
					$rootScope.alertError(error.fault.message);
					$scope.viewModel.isLoading = false;
				});					
		};
		

		//验证
		$scope.viewModel.validForm= function(isValid) {
            if (!isValid) {
                return;
            }else{
            		$scope.viewModel.rowCount=1;
                	$scope.viewModel.bindData();
            }
        };
        
        //验证补签复选框是否可用
        $scope.viewModel.isDisabled= function(raApproval) {
        	if(raApproval.approvalState==2
        			|| raApproval.approvalState==3
        			|| raApproval.isClosed==1){
        		return true;
        	}
        	return false;
        };
} ]);